#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef double db;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+ch-'0'; ch=getchar(); }
	return x*f;
}
const int MAXN=110*110;
int n,m,sum,head[MAXN*3],cnt,tot,_x[]={0,0,1,-1},_y[]={1,-1,0,0};
struct edge
{
	int v,next,val;
}e[MAXN*25];
void addedge(int x,int y,int z)
{
	e[cnt]=(edge){y,head[x],z};
	head[x]=cnt++;
	swap(x,y);
	e[cnt]=(edge){y,head[x],0};
	head[x]=cnt++;
	return;
}
struct MF
{
	int idx[MAXN*3],S,T,cur[MAXN*3];
	void bfs()
	{
		memset(idx,0,sizeof(idx));
		queue<int>q;
		q.push(S);
		idx[S]=1;
		while(!q.empty())
		{
			int u=q.front();q.pop();
			for(int i=head[u];~i;i=e[i].next)
			{
				int v=e[i].v;
				if(!e[i].val||idx[v])continue;
				idx[v]=idx[u]+1;
				q.push(v);
			}
		}
		return;
	}
	int dfs(int u,int _min)
	{
		if(u==T)return _min;
		int ret=0,k;
		for(int i=cur[u];~i;i=e[i].next)
		{
			int v=e[i].v;
			cur[u]=i;
			if(!e[i].val||idx[v]!=idx[u]+1)continue;
			k=dfs(v,min(_min-ret,e[i].val));
			e[i].val-=k;
			e[i^1].val+=k;
			ret+=k;
			if(ret==_min)break;
		}
		return ret;
	}
	int dinic()
	{
		int ret=0;
		while(true)
		{
			bfs();
			if(!idx[T])break;
			memcpy(cur,head,sizeof(head));
			ret+=dfs(S,INF);
		}
		return ret;
	}
}T1;
inline int ID(int x,int y)
{
	return (x-1)*m+y;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	n=read();m=read();
	memset(head,-1,sizeof(head));
	T1.T=3*n*m+1;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
		{
			int x=read();sum+=x;
			addedge(T1.S,ID(i,j),x);
		}
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
		{
			int x=read();sum+=x;
			addedge(ID(i,j),T1.T,x);
		}
	tot=n*m;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
		{
			int x=read();sum+=x;++tot;
			addedge(T1.S,tot,x);
			addedge(tot,ID(i,j),INF);
			for(int k=0;k<4;++k)
			{
				int xx=i+_x[k],yy=j+_y[k];
				if(!xx||!yy||xx>n||yy>m)continue;
				addedge(tot,ID(xx,yy),INF);
			}
		}
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
		{
			int x=read();sum+=x;++tot;
			addedge(tot,T1.T,x);
			addedge(ID(i,j),tot,INF);
			for(int k=0;k<4;++k)
			{
				int xx=i+_x[k],yy=j+_y[k];
				if(!xx||!yy||xx>n||yy>m)continue;
				addedge(ID(xx,yy),tot,INF);
			}
		}
	printf("%d\n",sum-T1.dinic());
	return 0;
}
